package com.handpay.zztong.hp.login.model.verifyversion;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.HttpUrlConstant;
import com.handpay.zztong.hp.config.ZZTConfig;

import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * Created by lyang on 2017/3/14.
 */

public class VerifyVersionModel extends BaseModel {
    public static final String ACCOUNT = "account";
    public static final String FRAGMENTFLAG = "fragmentFlag";
    public static final String PHONE = "phone";
    public static final String STATUS = "status";
    public static final String FAIL_DESCRIPTION = "failDescription";
    public static final String UPDATEURL = "updateurl";
    public static final String VERSION = "version";
    public static final String DESCRIPTION = "description";
    public static final String FORCEDUPDATE = "forcedUpdate";
    public static final String NOTICEINFO = "noticInfo";
    public static final String PAYTYPE = "paytype";
    private BusinessEngineListener mBusinessEngineListener;

    public boolean doVerifyVersionPost(ZZTongActivity activity, BusinessEngineListener listener) {
        mBusinessEngineListener = listener;
        Hashtable<String, String> params = new Hashtable<String, String>();
        params.put("version", ClientEngine.VERSION);
        params.put("platform", ClientEngine.MOBILETYPE);
        params.put("channel", ZZTConfig.UPDATE_CHANNEL);
        return doPost(activity, HttpUrlConstant.Do_ZztNewVerifyVersion, params, true, false);

    }

    @Override
    public boolean netResponse(String action,byte data[]) {
        boolean bResult = super.netResponse(action, data);
        if (bResult) {
            return true;
        }
        if (action.equals(HttpUrlConstant.Do_ZztNewVerifyVersion)) {
            VerifyVersionBean bean = null;
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    String result = new String(data, "UTF-8");
                    bean = FastJsonHelper.getObject(result, VerifyVersionBean.class);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else  {
                int responseCode = -1;
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, true);
                if (luaTable == null) {
                    return false;
                }
                Object rc = luaTable.rawget("responseCode");
                if (rc != null) {
                    if (rc instanceof String) {
                        responseCode = Integer.parseInt((String) rc);
                    } else {
                        responseCode = ((Double) rc).intValue();
                    }
                }
                if (responseCode == 0) {
//                    ClientEngine.getInstance().saveGlobal(ZZTongActivity.VerifyVersionOK, true);// 只用于登陆时，是否做了版本检测
                    String dgTrade = (String)luaTable.rawget("dgTrade");
                    String version = (String) luaTable.rawget("version");
                    String url = (String) luaTable.rawget("url");
                    String description = (String) luaTable.rawget("description");
                    String forcedUpdate = (String) luaTable.rawget("forcedUpdate");
                    bean = new VerifyVersionBean();
                    bean.setDgTrade(dgTrade);
                    bean.setVersion(version);
                    bean.setUrl(url);
                    bean.setDescription(description);
                    bean.setForcedUpdate(forcedUpdate);
                }
            }
            if (mBusinessEngineListener != null) {
                mBusinessEngineListener.callbackBusinessResponse( bean);
            }
        }
        return true;
    }


    @Override
    public void callbackJsonNetResponse(String action, ResponseBean resp, boolean showAlert) {

    }
}